#
# This file is part of Sonedyan.
# 
# Sonedyan is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 3 of the License, or (at your option) any
# later version.
#
# Sonedyan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public
# License along with Octave; see the file COPYING.  If not
# see <http://www.gnu.org/licenses/>.
# 
# Copyright (C) 2009-2014 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>
#

from igraph import *
from johnson import *

def unique(a):
    return list(set(a))

def intersection(a, b):
    return list(set(a) & set(b))

def union(a, b):
    return list(set(a) | set(b))

def difference(a, b):
    return list(set(a) - set(b))

def get_successors(g,vid,depth):
        svids = []
        svids = svids + g.successors(vid)
        depth = depth - 1
        while depth > 0:
                for i in svids:
                        svids = svids + g.successors(i)
                depth = depth - 1
        return unique(svids)

def get_predecessors(g,vid,depth):
        pvids = []
        pvids = pvids + g.predecessors(vid)
        depth = depth - 1
        while depth > 0:
                for i in pvids:
                        pvids = pvids + g.predecessors(i)
                depth = depth - 1
        return unique(pvids)

def get_v3_ids(g,c3):
        c3ids = []
        for c in c3:
                c3ids = c3ids + c
        return unique(c3ids)

def get_e3_ids(g,c3):
        e3ids = []
        for c in c3:
                e1 = g.es.select(_source=c[0],_target=c[1])[0].index
                e2 = g.es.select(_source=c[1],_target=c[2])[0].index
                e3 = g.es.select(_source=c[2],_target=c[0])[0].index
                e3ids.append(e1)
                e3ids.append(e2)
                e3ids.append(e3)
        return unique(e3ids)

def get_intersection_graph(g,c3):
        ig = Graph()
        for c in c3:
                n1 = g.vs(c[0])['name'][0]
                n2 = g.vs(c[1])['name'][0]
                n3 = g.vs(c[2])['name'][0]
                vn = n1+'.'+n2+'.'+n3
                ig.add_vertex(name=vn)
                #if 'conflict' in vn:
                #        ig.vs(name_eq=vn)['color'] = '#ff0000'
                #else:
                #        ig.vs(name_eq=vn)['color'] = '#00acad'

        c3.sort()
        for i1 in range(len(c3)-1):
                for i2 in range(i1, len(c3)):
                        if len(set(c3[i1])&set(c3[i2])) == 2:
                                n1a = g.vs(c3[i1][0])['name'][0]
                                n2a = g.vs(c3[i1][1])['name'][0]
                                n3a = g.vs(c3[i1][2])['name'][0]
                                n1b = g.vs(c3[i2][0])['name'][0]
                                n2b = g.vs(c3[i2][1])['name'][0]
                                n3b = g.vs(c3[i2][2])['name'][0]
                                vn1 = n1a+'.'+n2a+'.'+n3a
                                vn2 = n1b+'.'+n2b+'.'+n3b
                                ig.add_edge(vn1,vn2)
        return ig
